using System;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;
using Yoozoo.Gameplay.City;

namespace Yoozoo.Gameplay.CityEditor
{
    [CustomEditor(typeof(CityEventEditor))]
    public class CityEventEditorEditor : UnityEditor.Editor
    {
        private CityEventEditor _cityEventEditor;
        
        public static GUIContent eventHeaderText = EditorGUIUtility.TrTextContent("剩余事件", "");
        private SerializedProperty m_eventListProp;
        private ReorderableList m_eventRenderList;


        private SerializedProperty m_filterEventIdProp;
        
        private void OnEnable()
        {
            _cityEventEditor = target as CityEventEditor;
            
            m_eventListProp = serializedObject.FindProperty("_currentRemainEvents");
            m_eventRenderList = new ReorderableList(serializedObject, m_eventListProp, true, true, true, true);
            DrawEventListLayout(m_eventRenderList, m_eventListProp);
            _cityEventEditor.SetEventRenderList(m_eventRenderList);
            
            m_filterEventIdProp = serializedObject.FindProperty("filterEventId");
        }

        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            base.OnInspectorGUI();
            
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("开始编辑",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.Begin();
            }
            if (GUILayout.Button("结束编辑",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.End();
            }
            GUILayout.EndHorizontal();
            
            GUILayout.Space(20);
            
            GUILayout.BeginHorizontal();
            m_filterEventIdProp.intValue = EditorGUILayout.IntField("过滤ID", m_filterEventIdProp.intValue);
            if (GUILayout.Button("过滤"))
            {
                if (Application.isPlaying) _cityEventEditor.FilterEvent(_cityEventEditor.currentType,m_filterEventIdProp.intValue);
            }
            GUILayout.EndHorizontal();
            
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("全部",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.FilterEvent(-1);
            }
            if (GUILayout.Button("拾取事件",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.FilterEvent(1);
            }
            if (GUILayout.Button("征兵事件",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.FilterEvent(2);
            }
            GUILayout.EndHorizontal();
            
            GUILayout.BeginHorizontal();
            if (GUILayout.Button("RTS事件",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.FilterEvent(3);
            }
            if (GUILayout.Button("RPG事件",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.FilterEvent(4);
            }
            if (GUILayout.Button("区域解锁事件",GUILayout.Height(30)))
            {
                if (Application.isPlaying) _cityEventEditor.FilterEvent(5);
            }
            GUILayout.EndHorizontal();
            
            GUILayout.Space(20);
            
            m_eventRenderList.DoLayoutList();
            
            serializedObject.ApplyModifiedProperties();
        }

        private void DrawEventListLayout(ReorderableList list, SerializedProperty prop)
        {
            list.elementHeight = EditorGUIUtility.singleLineHeight * 2.5f;

            list.drawElementCallback = (Rect rect, int index, bool isActive, bool isFocused) =>
            {
                EditorGUIUtility.labelWidth = 40;
                var element = prop.GetArrayElementAtIndex(index);
                rect.y += 2;
                int eventId = element.intValue;

                AreaEventConfig.AreaEventElement areaEventElement = _cityEventEditor.GetEventElement(eventId);
                Rect idRect = new Rect(rect.x, rect.y, 190, EditorGUIUtility.singleLineHeight);
                EditorGUI.LabelField(idRect, "事件ID", areaEventElement.id.ToString());
                
                rect.y += EditorGUIUtility.singleLineHeight;
                Rect infoRect = new Rect(rect.x, rect.y, 250, EditorGUIUtility.singleLineHeight);
                EditorGUI.LabelField(infoRect, "信息",    "占地大小："+ areaEventElement.floor_space);
            };
            
            list.onSelectCallback = (ReorderableList l) =>
            {
                _cityEventEditor.SelectEvent(l.index);
            };
            
            list.drawHeaderCallback = (Rect rect) =>
            {
                EditorGUI.LabelField(rect, eventHeaderText);
            };
            
            list.onCanRemoveCallback = li => { return false; };
            list.onCanAddCallback = li => { return false;};
        }
        
    }
}